package com.panda.git;


import com.panda.command.Console;
import com.panda.pojo.Block;
import com.panda.pojo.Component;
import com.panda.pojo.Element;
import com.panda.pojo.Pr;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.util.List;
import java.util.Map;

class NetworkUtils {
    private static RestTemplate restTemplate;

    static {

        restTemplate = new RestTemplate();


    }


    //发起push请求
    static boolean push(String group, String root, String version, Pr pr) {


        removeRedundant(pr);

        try {

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(pr);
            restTemplate.postForObject("http://localhost/push?group={group}&root={root}&version={version}", byteArrayOutputStream.toByteArray(), Object.class, group, root, version);

            objectOutputStream.close();

            return true;
        } catch (IOException e) {

            Console.err(e.getMessage());
            return false;
        }

    }


    private static void removeRedundant(Pr pr) {

        Map<String, Component> componentMap = pr.getComponentMap();

        //清空所有不必要的内容
        for (String s : componentMap.keySet()) {

            Component component = componentMap.get(s);

            component.setContext(null);

            component.getScope().setRealLocalParams(null);

            //不能简单的这样处理,需要深度递归移除指向
            component.getScope().setParent(null);

            component.getScope().setAllParams(null);

            component.setLastSubElement(null);
            component.setBufferedText(null);
            component.setParamsText(null);
            component.setBodyText(null);

            try {

                //Login 和Search有问题


                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
                objectOutputStream.writeObject(component);

                objectOutputStream.close();

            } catch (IOException e) {

                Console.err(e.getMessage());

//                find(component);

            }

        }

    }
/*


    private static void find(Element element) {

        List<Block> subBlocks = element.getSubBlocks();

        if (subBlocks != null)
            for (Block block : subBlocks) {

                if (block instanceof Element) {


                    try {

                        //Login 和Search有问题


                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
                        objectOutputStream.writeObject(block);

                        objectOutputStream.close();

                    } catch (IOException e) {

                        System.out.println("异常:" + ((Element) block).getName());


                        find((Element) block);
                    }
                }

            }

    }

*/

    static Pr pull(String group, String root, String version) {

        try {

            byte[] templateForObject = restTemplate.getForObject("http://localhost/pull/{group}/{root}/{version}", byte[].class, group, root, version);

            if (templateForObject == null) {
                return null;
            }

            ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(templateForObject));
            Pr r = (Pr) objectInputStream.readObject();
            objectInputStream.close();

            return r;


        } catch (Exception e) {

            e.printStackTrace();

            Console.err(e.getMessage());
            return null;
        }

    }


}
